﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MealCardManagement.Client
{
    public partial class CardList : UserControl
    {
        public CardList()
        {
            InitializeComponent();
        }

        private void CardList_Resize(object sender, EventArgs e)
        {
            listViewCard.Columns[0].Width = 40;
            listViewCard.Columns[1].Width = 140;
            listViewCard.Columns[2].Width = listViewCard.Width - 180 - 4;
        }

        public void SetCards(IEnumerable<MealCard> cards)
        {
            listViewCard.Items.Clear();
            foreach (var card in cards)
            {
                var item = new ListViewItem(card.Id.ToString());
                item.SubItems.Add(card.CreatTime.ToString("yyyy-MM-dd hh:mm:ss"));
                item.SubItems.Add(card.Money.ToString());
                listViewCard.Items.Add(item);
            }
        }

        private void NewCard_Click(object sender, EventArgs e)
        {
            NewCard?.Invoke(this);
        }

        private void listViewCard_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cardSelected = SelectedCardId != null;
            btnDeleteCard.Enabled = cardSelected;
            btnRecharge.Enabled = cardSelected;
            btnRecharge.Enabled = cardSelected;
        }

        private void btnDeleteCard_Click(object sender, EventArgs e)
        {
            DeleteCard?.Invoke(this, SelectedCardId.Value);
        }

        private void btnRecharge_Click(object sender, EventArgs e)
        {
            using (var moneyInput = new MoneyInputForm())
            {
                moneyInput.Text = "充值";
                if (moneyInput.ShowDialog() != DialogResult.OK)
                    return;
                var value = moneyInput.InputMoney;
                RechargeCard?.Invoke(this, SelectedCardId.Value, value);
            }
        }

        private void btnConsume_Click(object sender, EventArgs e)
        {
            using (var moneyInput = new MoneyInputForm())
            {
                moneyInput.Text = "消费";
                if (moneyInput.ShowDialog() != DialogResult.OK)
                    return;
                var value = moneyInput.InputMoney;

                var cardMoney = decimal.Parse(listViewCard.SelectedItems[0].SubItems[2].Text);
                if (value > cardMoney)
                {
                    MessageBox.Show("余额不足");
                    return;
                }

                ConsumeCard?.Invoke(this, SelectedCardId.Value, value);
            }
        }

        public event Action<CardList> NewCard;
        public event Action<CardList, int> DeleteCard;
        public event Action<CardList, int, decimal> RechargeCard;
        public event Action<CardList, int, decimal> ConsumeCard;

        public int? SelectedCardId
        {
            get
            {
                if (listViewCard.SelectedItems.Count == 0)
                    return null;
                else
                    return int.Parse(listViewCard.SelectedItems[0].Text);
            }
        }
    }
}
